package com.dm.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dm.common.JsonResult;
import com.dm.entity.Dormitory;
import com.dm.entity.Manager;
import com.dm.service.IDormitoryService;
import com.dm.service.IManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dancun
 * @since 2021-05-02
 */
@Slf4j
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    IDormitoryService dormitoryService;
    @Autowired
    IManagerService managerService;

    @PostMapping("/save")
    @ApiOperation("")
    public JsonResult save(@RequestBody Dormitory dormitory){
        try{
            dormitoryService.save(dormitory);
            LambdaUpdateWrapper<Manager> wrapper = Wrappers.lambdaUpdate();
            wrapper.set(Manager::getDid,dormitory.getDid()).eq(Manager::getUname,dormitory.getManager().getUname());
            managerService.update(wrapper);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess();
    }
    @PostMapping("/delete")
    @ApiOperation("")
    public JsonResult delete(@RequestBody Dormitory dormitory){
        try{
            dormitoryService.removeById(dormitory.getDid());
        }catch (Exception e){
           e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess();
    }
    @PostMapping("/update")
    @ApiOperation("")
    public JsonResult update(@RequestBody Dormitory dormitory){
        try{
            LambdaUpdateWrapper<Manager> wrapper = Wrappers.lambdaUpdate();
            wrapper.set(Manager::getDid,dormitory.getDid()).eq(Manager::getUname,dormitory.getManager().getUname());
            managerService.update(wrapper);
            dormitoryService.updateById(dormitory);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess();
    }

    @PostMapping("/list")
    @ApiOperation("")
    public JsonResult List(@RequestBody JSONObject jsonObject){
        Integer pageSize =null;
        Integer pageNum =null;

        try{
            pageSize = Integer.parseInt(jsonObject.getString("pageSize"));
            pageNum = Integer.parseInt(jsonObject.getString("pageNum"));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=10;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;

        try{
            page = new PageInfo<Dormitory>(dormitoryService.listManager());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess(page);
    }
    @PostMapping("/find")
    @ApiOperation("")
    public JsonResult find(@RequestBody Integer id){
        Dormitory dormitory = null;
        try{
            dormitory = dormitoryService.getOneDormitory(id);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess(dormitory);
    }

    @PostMapping("/search")
    @ApiOperation("")
    public JsonResult search(@RequestBody JSONObject jsonObject){
        Integer pageSize =null;
        Integer pageNum =null;

        try{
            pageSize = Integer.parseInt(jsonObject.getString("pageSize"));
            pageNum = Integer.parseInt(jsonObject.getString("pageNum"));
        }catch (Exception e){
            e.printStackTrace();
        }
        String key = jsonObject.getString("key");
        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=10;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;

        try{
            LambdaQueryWrapper<Dormitory> wrapper = Wrappers.lambdaQuery();
            wrapper.like(Dormitory::getDname,key).or().like(Dormitory::getDid,key);

            page = new PageInfo<Dormitory>(dormitoryService.list(wrapper));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess(page);
    }

}

